package com.imooc.o2o.interceptor.frontend;

import com.imooc.o2o.entity.PersonInfo;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

public class UserLoginInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object UserObject=request.getSession().getAttribute("user");
        if(UserObject!=null){
            PersonInfo personInfo=(PersonInfo)UserObject;
            if(personInfo!=null&&personInfo.getUserType()==1){
                return true;
            }
        }
        //不满足条件则进行登录跳转
        PrintWriter out=response.getWriter();
        out.println("<html>");
        out.println("<script>");
        out.println("window.open ('" + request.getContextPath() + "/local/login?usertype=1','_self')");
        out.println("</script>");
        out.println("</html>");
        return false;
    }
}
